package com.akw.boot3.redis.controller;

import com.akw.boot3.redis.entity.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class RedisTestController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/count")
    public String count() {
        Long hello = stringRedisTemplate.opsForValue().increment("hello");
        return "访问了[" + hello + "]次";
    }

    @GetMapping("/person/save")
    public Person savePerson() throws JsonProcessingException {
        Person person = new Person(1L, "AKW", 18, new Date());
        // 将person 转成JSON字符串
        String personString = new ObjectMapper().writeValueAsString(person);
        stringRedisTemplate.opsForValue().set("person", personString);
        return person;
    }
}
